package com.xmx.productcenter.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiOperation;
import com.xmx.productcenter.entity.GoodsEntity;
import com.xmx.productcenter.service.GoodsService;
import com.xmx.common.utils.PageUtils;
import com.xmx.common.utils.R;
import io.swagger.annotations.Api;


/**
 * 商品基本信息表
 *
 * @author fbb
 * @email boyinbing@126.com
 * @date 2021-08-11 01:24:55
 */
@Api(tags = "GoodsController", description = "商品基本信息表服务")
@RestController
@RequestMapping("productcenter/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("productcenter:goods:list")
    @ApiOperation("获取所有商品基本信息表信息")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("productcenter:goods:info")
    @ApiOperation("获取指定id的商品基本信息表详情")
    public R info(@PathVariable("id") Integer id){
		GoodsEntity goods = goodsService.getById(id);

        return R.ok().put("goods", goods);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("添加商品基本信息表信息")
    //@RequiresPermissions("productcenter:goods:save")
    public R save(@RequestBody GoodsEntity goods){
		goodsService.save(goods);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation("更新指定id商品基本信息表信息")
    //@RequiresPermissions("productcenter:goods:update")
    public R update(@RequestBody GoodsEntity goods){
		goodsService.updateById(goods);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除指定id商品基本信息表信息")
    //@RequiresPermissions("productcenter:goods:delete")
    public R delete(@RequestBody Integer[] ids){
		goodsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/count")
    @ApiOperation("总数")
    public R count(){
        int count = goodsService.count();
        return R.ok().put("data",count);
    }


    @GetMapping("/detail")
    public R detail(@RequestParam("id") Integer id){
        GoodsEntity byId = goodsService.getById(id);
        return R.ok().put("info",byId);
    }

    @GetMapping("/related")
    public PageUtils pageUtils(@RequestParam("id")Integer id){
        int pageNo=1;
        int pageSize=6;
        IPage<GoodsEntity> goodsEntityIPage = goodsService.related(new Page<GoodsEntity>(pageNo,pageSize).addOrder(OrderItem.desc("add_time")),id);
        return new PageUtils(goodsEntityIPage);
    }

    @GetMapping("/getBrandlist")
    public R getBrandList(@RequestParam("brandId")Integer brandId,Integer page,Integer limit){
        QueryWrapper<GoodsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(GoodsEntity::getBrandId,brandId);

        Page page1 = goodsService.page(new Page(page, limit), queryWrapper);

        Map data = new HashMap();
        data.put("total",page1.getTotal());
        data.put("pages",1);
        data.put("limit",page1.getCurrent());
        data.put("page",1);
        data.put("list",page1.getRecords());
        return R.ok().put("data",data);
    }

}
